<?php


namespace app\tt\controller;


 use app\user\logic\ShareLinkLogic;
 use think\Console;
use think\Controller;
use think\Db;
use think\facade\Request;

class Ad extends  Controller
{
    protected $middleware = [
        'app\http\middleware\checkTTUserToken::class'=>['except'=>['addAdPlayerHistory_ks']]
    ];

    //增加广告播放记录(快手)
    public function addAdPlayerHistory_ks()
    {
        // promote_user_id 推广者的id	   tt_user_id 观看者的id  	product_id 所属的产品id	ip 广告观看者的ip地址	add_time 时间
        //获取用户的ip
        $ip=   $this->getClientIp();
        $user_id=Request::param('user_id',0);
        $promote_user_id=Request::param('promote_user_id');
        $mini_app_code=Request::param('mini_app_code','kuaishou');
        $url_no=Request::param('url_no',0);
        $product_id=Request::param('product_id',0);     //产品的id   可能为剧的id  也可能为

            //添加观看记录
            $res= Db::table('ad_play_history')->insert([
                "promote_user_id"=>$promote_user_id,
                "tt_user_id"=>$user_id,
                "mini_app_code"=>$mini_app_code,
                "product_id"=>$product_id,
                "ip"=>$ip,
                "add_time"=>time()
            ]);

            //增加播放次数
            ShareLinkLogic::addViews($url_no,0);
            $this->result('',200,'成功');

    }



    //增加广告播放记录
    public function addAdPlayerHistory()
    {
        // promote_user_id 推广者的id	   tt_user_id 观看者的id  	product_id 所属的产品id	ip 广告观看者的ip地址	add_time 时间
         //获取用户的ip
        $ip=   $this->getClientIp();
        $user_id=Request::param('user_id');
        $promote_user_id=Request::param('promote_user_id');
        $mini_app_code=Request::param('mini_app_code');
        $url_no=Request::param('url_no');
        $product_id=Request::param('product_id',0);     //产品的id   可能为剧的id  也可能为

        $start_time=strtotime(date("Y-m-d",time()));
        $end_time=$start_time+60*60*24;

        //用户今天所有的观看次数
        $count=Db::table('ad_play_history')->where([
            ['tt_user_id','=',$user_id],
            ['add_time','BETWEEN',[$start_time,$end_time]]
        ])->count('id');

        //同ip的观看次数
        $ip_count=Db::table('ad_play_history')->where([
            ['ip','=',$ip],
            ['add_time','BETWEEN',[$start_time,$end_time]]
        ])->count('id');

        //单用户观看大于两次，或者同ip大于30次就不做记录
        if($count>=2 || $ip_count>=30){
            $this->result($ip,200,'今日次数用完了');
        }else{

            //添加观看记录
           $res= Db::table('ad_play_history')->insert([
               "promote_user_id"=>$promote_user_id,
               "tt_user_id"=>$user_id,
               "mini_app_code"=>$mini_app_code,
               "product_id"=>$product_id,
               "ip"=>$ip,
               "add_time"=>time()
           ]);

           //增加播放次数
            ShareLinkLogic::addViews($url_no);
            $this->result('',200,'成功');
        }
    }

    //添加log
    public function setLog()
    {
        $data= Request::only(['key','value']);

        //插入数据
        $res= Db::table('logs')->insert($data);

        $this->result($res,200,'成功');

    }


    private   function getClientIp()
    {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        if (getenv('HTTP_X_REAL_IP')) {
            $ip = getenv('HTTP_X_REAL_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
            $ips = explode(',', $ip);
            $ip = $ips[0];
        } elseif (getenv('REMOTE_ADDR')) {
            $ip = getenv('REMOTE_ADDR');
        } else {
            $ip = '0.0.0.0';
        }

        return $ip;
    }

}
